package com.ewanghuiju.app.presenter.login;

import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.login.AddInvitecodeContract;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.InviterInfo;
import com.ewanghuiju.app.model.bean.response.LoginResponBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.http.request.BaseRequest;
import com.ewanghuiju.app.model.http.response.MyHttpResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import javax.inject.Inject;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/14
 */
public class AddInvitecodePresenter extends RxPresenter<AddInvitecodeContract.View> implements AddInvitecodeContract.Presenter {
    private DataManager mDataManager;

    @Inject
    public AddInvitecodePresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getInviterinfo(BaseRequest baseRequest) {
        addSubscribe(mDataManager.getInviterinfo(baseRequest)
                        .compose(RxUtil.<MyHttpResponse<InviterInfo>>rxSchedulerHelper())
                        .compose(RxUtil.<InviterInfo>handleMyResult())
                        .subscribeWith(new CommonSubscriber<InviterInfo>(mView, true, false) {
                            @Override
                            public void onNext(InviterInfo inviterInfo) {
                                if (mView != null)
                                    mView.showInviterinfo(inviterInfo);
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
//                                if (e instanceof ApiException) {
//                                    mView.showInviterinfoErrorMsg(((ApiException) e).getCode(), e.getMessage());
//                                } else {
//                                    mView.showInviterinfoErrorMsg(Constants.NO_KNOW_EROO, e.getMessage());
//                                }
                            }
                        })
        );

    }

    @Override
    public void addCode(String code) {

    }

    /**
     * 绑定邀请码
     *
     * @param mobile
     * @param number
     */
    @Override
    public void bindUserMobileparentid(String mobile, String number) {

        addSubscribe(mDataManager.userMobileparentid(mobile, number)
                .compose(RxUtil.<MyHttpResponse<LoginResponBean>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<LoginResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<LoginResponBean> optional) {
                        if (mView != null)
                            mView.showBindUserMobileInfo(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    /**
     * 获取用户信息
     */
    @Override
    public void getUserInfo() {
        addSubscribe(mDataManager.getUserInfo()
                .compose(RxUtil.<MyHttpResponse<UserInfoResponBean>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<UserInfoResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<UserInfoResponBean> userInfo) {
                        if (mView != null) {
                            if (userInfo.getIncludeNull() != null) {
                                mDataManager.insertUserInfoResponBean(userInfo.getIncludeNull());
                            }
                            mView.showUserInfo(userInfo.getIncludeNull());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    /**
     * 微信注册
     *
     * @param unionid    unionid
     * @param mobile     手机号
     * @param number     邀请码
     * @param headimgurl 头像
     * @param nickname   昵称
     */
    @Override
    public void wxUserRegister(String unionid, String mobile, String number, String headimgurl, String nickname) {

        addSubscribe(mDataManager.wxUserRegister(unionid, mobile, number, headimgurl, nickname)
                .compose(RxUtil.<MyHttpResponse<LoginResponBean>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<LoginResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<LoginResponBean> userInfo) {
                        if (mView != null) {
                            mView.showWxUserRegister(userInfo.getIncludeNull());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );

    }
}
